/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
  //
// Copyright (c) 2006 Georgia Tech Research Corporation
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//

// ns3 - On/Off Data Source Application class
// George F. Riley, Georgia Tech, Spring 2007
// Adapted from ApplicationFlow in GTNetS.

#include "flow-server.h"

NS_LOG_COMPONENT_DEFINE ("FlowServer");

using namespace std;

namespace ns3 {

NS_OBJECT_ENSURE_REGISTERED (FlowServer);
 
FlowServer::FlowServer ( Ptr<Socket> socket) :
m_flowSize(0),
m_dataToSend(0),
m_flowFinished(0)
{
  NS_LOG_FUNCTION_NOARGS ();
  m_startTime = Simulator::Now();

  m_socket = socket;
  m_socket->SetRecvCallback (MakeCallback(&FlowServer::HandleRead, this));
  m_socket->SetDataSentCallback(MakeCallback(&FlowServer::HandleSend, this));
  m_socket->SetCloseCallbacks (
            MakeCallback(&FlowServer::HandleClose, this),
            MakeCallback(&FlowServer::HandleError, this));

}                 
 
FlowServer::~FlowServer()
{
  NS_LOG_FUNCTION_NOARGS ();
  m_socket = 0;
}


void 
FlowServer::Stop ()
{
  if ( m_socket )
    m_socket->Close();
  m_socket = 0;
}      

void 
FlowServer::HandleRead(Ptr<Socket> socket)
{
  NS_LOG_FUNCTION_NOARGS ();
  Ptr<Packet> packet;
  Address from;

  while (packet = socket->RecvFrom (from))
  {
    if ( !m_dataToSend )
    {
      // retrieve size from first packet
      // TODO: could use packet tag? 
      uint8_t buffer[packet->GetSize()];
      packet->CopyData(buffer, packet->GetSize());

      for ( int8_t i = 3; i >= 0 ; i-- )
      {
        m_dataToSend <<= 8;
        m_dataToSend |= buffer[i];
      }
    }
  }    

  m_flowSize = m_dataToSend;
  NS_LOG_LOGIC(from << " requested " << m_flowSize << " bytes");
  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());

  //start sending packets
  HandleSend(socket,0);
}
 
void 
FlowServer::HandleSend(Ptr<Socket> socket, uint32_t dataSent) 
{ 
  NS_LOG_FUNCTION (this << m_dataToSend << dataSent); 

  if( m_dataToSend )
  {
    uint32_t sent = min(m_dataToSend, m_socket->GetTxAvailable() );
    NS_LOG_FUNCTION(sent);
    Ptr<Packet> p = Create<Packet> ( sent );
    socket->Send(p);
    m_dataToSend -= sent;
  }

  //we only do this because NSC gets stuck in CLOSE_WAIT unless we're explicit
  //if (m_dataSent==m_flowSize)
  //{
    //socket->Close();
}
         
void
FlowServer::HandleClose ( Ptr<Socket> socket )
{         
  NS_LOG_FUNCTION_NOARGS ();
  if (!m_flowFinished.IsNull ())
  {
    m_flowFinished (this);
  }
}
  
void
FlowServer::HandleError ( Ptr<Socket> socket )
{         
  NS_LOG_FUNCTION_NOARGS ();
}

void
FlowServer::SetFlowFinishCallback (Callback<void, Ptr<FlowServer> > flowFinished )
{         
  m_flowFinished = flowFinished;
}
  
uint32_t
FlowServer::GetSize()
{
  return m_flowSize;
}

Time
FlowServer::GetDuration()
{
  return Simulator::Now() - m_startTime;
}

} // Namespace ns3
